package com.sc.nft.util;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class WeChatUtil  implements InitializingBean {

    @Autowired
    private Environment environment;

    private static String env;

    public static void sendWeChatMessage(String message) {
        if(!env.equals("prod")){
            return;
        }
        String weChatToken = getWeChatToken();
        String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=";
        url = url+weChatToken;
        JSONObject pram = new JSONObject();

        pram.put("touser","LiuXiangDong");
//        pram.put("touser","LiuXiangDong|ZhangXiangYu|WangWeiCheng");
        pram.put("msgtype","text");
        pram.put("agentid","1000004");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("content",message);
        pram.put("text",jsonObject);
        HttpUtil.post(url, pram.toJSONString());
    }

    public static void sendLiGenWeChatMessage(String message) {

        String weChatToken = getWeChatToken();
        String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=";
        url = url+weChatToken;
        JSONObject pram = new JSONObject();

        pram.put("touser","ligen");
        pram.put("msgtype","text");
        pram.put("agentid","1000004");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("content",message);
        pram.put("text",jsonObject);
        HttpUtil.post(url, pram.toJSONString());
    }

    private static String getWeChatToken(){
        String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww55e4c231bb17f3a5&corpsecret=BKLR5aLNaQCcP0VHSC0FHFf1t48ZZgkGtoc2l-o5Mds";
        String s = HttpUtil.get(url);
        JSONObject jsonObject = JSONObject.parseObject(s);
        String accessToken = jsonObject.get("access_token").toString();
        return accessToken;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        WeChatUtil.env=environment.getProperty("spring.profiles.active");
    }
}
